package hos.java.util;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import hos.java.compat.CalendarConvert;
import hos.java.func.FunctionReturn;

/**
 * <p>Title: ListUtlis </p>
 * <p>Description: 集合工具类 </p>
 * <p>Company: www.Listuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2021/3/22 14:21
 */
public class ListUtils {

    /**
     * 克隆数组
     *
     * @param <T>   数组元素类型
     * @param array 被克隆的数组
     * @return 新数组
     */
    public static <T> T[] clone(T[] array) {
        if (array == null) {
            return null;
        }
        return array.clone();
    }

    public static <T> String split(List<T> list, String split) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        final StringBuilder builder = new StringBuilder();
        final int size = list.size();
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                builder.append(split);
            }
            builder.append(list.get(i));
        }
        return builder.toString();
    }

    public static <T> String split(T[] list, String split) {
        if (list == null || list.length == 0) {
            return "";
        }
        final StringBuilder builder = new StringBuilder();
        final int size = list.length;
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                builder.append(split);
            }
            builder.append(list[i]);
        }
        return builder.toString();
    }

    /**
     * 转换为Integer列表<br>
     *
     * @param split 分隔符
     * @param split 被转换的值
     * @return 结果
     */
    public static List<Integer> toIntList(String str, String split) {
        List<Integer> list = new ArrayList<>();
        if (str == null || str.length() == 0) {
            return list;
        }
        String[] arr = str.split(split);
        for (String ss : arr) {
            list.add(Integer.parseInt(ss));
        }
        return list;
    }

    /**
     * 將字字符串分割成Long類型的列表
     *
     * @param str
     * @return
     */
    public static List<Long> toLongList(String str, String split) {
        List<Long> list = new ArrayList<>();
        String[] splits = str.split(split);
        for (String s : splits) {
            list.add(Long.parseLong(s));
        }
        return list;
    }

    public static List<Double> toDoubleList(String str, String split) {
        List<Double> list = new ArrayList<>();
        String[] splits = str.split(split);
        for (String s : splits) {
            list.add(Double.parseDouble(s));
        }
        return list;
    }

    public static List<Float> toFloatList(String str, String split) {
        List<Float> list = new ArrayList<>();
        String[] splits = str.split(split);
        for (String s : splits) {
            list.add(Float.parseFloat(s));
        }
        return list;
    }

    public static List<Boolean> toBooleanList(String str, String split) {
        List<Boolean> list = new ArrayList<>();
        String[] splits = str.split(split);
        for (String s : splits) {
            list.add(Boolean.parseBoolean(s));
        }
        return list;
    }

    public static List<String> toStringList(String str, String split) {
        List<String> list = new ArrayList<>();
        String[] splits = str.split(split);
        for (String s : splits) {
            list.add(String.valueOf(s));
        }
        return list;
    }


    public static int getLength(Object array) {
        return array == null ? 0 : Array.getLength(array);
    }

    public static <V> V getIfNull(List<V> target, int index, V defaultValue) {
        if (target == null || target.size() == 0 || target.size() <= index) {
            return defaultValue;
        }
        V v = target.get(index);
        if (v == null) {
            return defaultValue;
        } else if (("null".equals(v) || "NULL".equals(v) || "Null".equals(v))) {
            return defaultValue;
        } else {
            return v;
        }
    }

    public static <V> V toNull(List<V> target, int index) {
        return getIfNull(target, index, null);
    }

    public static <V> String toNullString(List<V> target, int index) {
        V v = toNull(target, index);
        if (v == null) {
            return null;
        }
        return AnyUtils.empty(v);
    }


    public static <V> String empty(List<V> target, int index, String start, String end, String defaultValue) {
        String value = toNullString(target, index);
        if (value == null) {
            return start + defaultValue + end;
        }
        return start + value + end;
    }

    public static <V> String empty(List<V> target, int index, String start, String end, FunctionReturn<String> defaultValue) {
        String value = toNullString(target, index);
        if (value == null) {
            if (defaultValue == null) {
                return start + end;
            }
            return start + defaultValue.invoke() + end;
        }
        return start + value + end;
    }

    public static <V> String empty(List<V> target, int index, String defaultValue) {
        return empty(target, index, "", "", defaultValue);
    }

    public static <V> String empty(List<V> target, int index, FunctionReturn<String> supplier) {
        return empty(target, index, "", "", supplier);
    }

    public static <V> String empty(List<V> target, int index) {
        return empty(target, index, "");
    }

    public static <V> String divide(List<V> target, int index) {
        return empty(target, index, "-");
    }

    public static <V> String toEnd(List<V> target, int index, String end, String value) {
        return empty(target, index, "", end, value);
    }

    public static <V> String toEnd(List<V> target, int index, String end) {
        return toEnd(target, index, end, "");
    }

    public static <V> String toStart(List<V> target, int index, String start) {
        return empty(target, index, start, "", "");
    }

    public static <V> String rate(List<V> target, int index) {
        return rate(target, index, "-");
    }

    public static <V> String rate(List<V> target, int index, String defaultNull) {
        return empty(target, index, "", "%", defaultNull);
    }

    public static <V> String like(List<V> target, int index) {
        String like = toNullString(target, index);
        if (like == null || like.isEmpty()) {
            return "%%";
        }
        return "%" + like + "%";
    }

    //<editor-fold desc="Number">
    public static <V> Integer asInteger(List<V> target, int index, Integer defaultValue) {
        return AnyUtils.asInteger(toNull(target, index), defaultValue);
    }

    public static <V> int asInteger(List<V> target, int index) {
        return AnyUtils.asInteger(toNull(target, index));
    }

    public static <V> Integer toNullInteger(List<V> target, int index) {
        return AnyUtils.toNullInteger(toNull(target, index));
    }

    public static <V> String toNullIntegerString(List<V> target, int index) {
        return AnyUtils.toNullIntegerString(toNull(target, index));
    }

    public static <V> String asIntegerString(List<V> target, int index, Integer defaultValue) {
        return AnyUtils.asIntegerString(toNull(target, index), defaultValue);
    }

    public static <V> String asIntegerString(List<V> target, int index) {
        return AnyUtils.asIntegerString(toNull(target, index));
    }

    public static <V> Long asLong(List<V> target, int index, Long defaultValue) {
        return AnyUtils.asLong(toNull(target, index), defaultValue);
    }

    public static <V> long asLong(List<V> target, int index) {
        return AnyUtils.asLong(toNull(target, index));
    }

    public static <V> Long toNullLong(List<V> target, int index) {
        return AnyUtils.toNullLong(toNull(target, index));
    }

    public static <V> String toNullLongString(List<V> target, int index) {
        return AnyUtils.toNullLongString(toNull(target, index));
    }

    public static <V> String asLongString(List<V> target, int index, Long defaultValue) {
        return AnyUtils.asLongString(toNull(target, index), defaultValue);
    }

    public static <V> String asLongString(List<V> target, int index) {
        return AnyUtils.asLongString(toNull(target, index));
    }

    public static <V> Boolean asBoolean(List<V> target, int index, Boolean defaultValue) {
        return AnyUtils.asBoolean(toNull(target, index), defaultValue);
    }

    public static <V> boolean asBoolean(List<V> target, int index) {
        return AnyUtils.asBoolean(toNull(target, index));
    }

    public static <V> Boolean toNullBoolean(List<V> target, int index) {
        return AnyUtils.toNullBoolean(toNull(target, index));
    }

    public static <V> String toNullBooleanString(List<V> target, int index) {
        return AnyUtils.toNullBooleanString(toNull(target, index));
    }

    public static <V> String asBooleanString(List<V> target, int index, Boolean defaultValue) {
        return AnyUtils.asBooleanString(toNull(target, index), defaultValue);
    }

    public static <V> String asBooleanString(List<V> target, int index) {
        return AnyUtils.asBooleanString(toNull(target, index));
    }

    public static <V> Double asDouble(List<V> target, int index, Double defaultValue) {
        return AnyUtils.asDouble(toNull(target, index), defaultValue);
    }

    public static <V> double asDouble(List<V> target, int index) {
        return AnyUtils.asDouble(toNull(target, index));
    }

    public static <V> Double toNullDouble(List<V> target, int index) {
        return AnyUtils.toNullDouble(toNull(target, index));
    }

    public static <V> String toNullDoubleString(List<V> target, int index) {
        return AnyUtils.toNullDoubleString(toNull(target, index));
    }

    public static <V> String asDoubleString(List<V> target, int index, Double defaultValue) {
        return AnyUtils.asDoubleString(toNull(target, index), defaultValue);
    }

    public static <V> String asDoubleString(List<V> target, int index) {
        return AnyUtils.asDoubleString(toNull(target, index));
    }
    //</editor-fold>

    public static <V> String timeNull(List<V> target, int index) {
        return time(target, index, null);
    }

    public static <V> String time(List<V> target, int index) {
        return time(target, index, "");
    }

    public static <V> String time(List<V> target, int index, String defaultNull) {
        Object orNull = toNull(target, index);
        if (orNull == null) {
            return defaultNull;
        }
        return CalendarConvert.toEmpty(empty(target, index));
    }


    public static <V> Calendar calendar(List<V> target, int index, Calendar calendar) {
        String time = timeNull(target, index);
        if (time == null) {
            return calendar;
        }
        return CalendarConvert.string2Calendar(time);
    }

    public static <V> Calendar calendar(List<V> target, int index) {
        return calendar(target, index, new GregorianCalendar());
    }

    public static <V> String addLength(List<V> target, int index, String defaultNull, int allLength) {
        String orString = toNullString(target, index);
        if (orString == null) {
            orString = defaultNull;
        }
        return StringUtils.addLength(orString, allLength);
    }

    public static <V> String addLengthCenter(List<V> target, int index, String defaultNull, int allLength) {
        String value = toNullString(target, index);
        if (value == null) {
            value = defaultNull;
        }
        return StringUtils.addLengthCenter(value, allLength);
    }

    public static <V> String addLength(List<V> target, int index, int allLength) {
        return addLength(target, index, "", allLength);
    }

    public static <V> String addLengthCenter(List<V> target, int index, int allLength) {
        return addLengthCenter(target, index, "", allLength);
    }
}
